<template>
  <div>
    <div class="common-form">
      <span>{{ curItem.name }}</span>
    </div>
    <el-form size="small" :model="curItem" label-width="100px">
      <div class="f16 gray3 form-subtitle">{{ $t('page.product_setting') }}</div>
      <el-form-item :label="$t('page.product_source')">
        <el-radio-group v-model="curItem.params.source">
          <el-radio-button :label="'auto'">{{ $t('page.auto_fetch') }}</el-radio-button>
          <el-radio-button :label="'choice'">{{ $t('page.manual_select') }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <!-- 自动获取 -->
      <template v-if="curItem.params.source == 'auto'">
        <!-- 商品分类 -->
        <el-form-item :label="$t('page.product_category')" v-if="CategoryList && CategoryList.length > 0">
          <el-cascader class="ww100" v-model="currCategory" ref="cascader" :options="CategoryList"
            :props="{ checkStrictly: true, children: 'child', value: 'categoryId', label: 'name' }"
            @change="changeCategory"></el-cascader>
        </el-form-item>
        <!-- 商品排序 -->
        <el-form-item :label="$t('page.product_sort')">
          <el-radio-group v-model="curItem.params.auto.productSort">
            <el-radio-button :label="'all'">{{ $t('page.sort_comprehensive') }}</el-radio-button>
            <el-radio-button :label="'sales'">{{ $t('page.sort_sales') }}</el-radio-button>
            <el-radio-button :label="'price'">{{ $t('page.sort_price') }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <!-- 显示数量 -->
        <el-form-item :label="$t('page.display_count_label')"><el-input v-model="curItem.params.auto.showNum"
            class="w-auto"></el-input></el-form-item>
      </template>
      <!-- 手动选择 -->
      <template v-if="curItem.params.source == 'choice'">
        <el-form-item :label="$t('page.product_list')">
          <draggable v-model="curItem.data" :options="{ draggable: '.item', animation: 500 }"
            class="choice-product-list">
            <template #item="{ element, index }">
              <div class="d-s-c f-w">
                <div class="item">
                  <div class="delete-box">
                    <el-icon :size="20" @click="$parent.onEditorDeleleData(index, selectedIndex)">
                      <CircleCloseFilled />
                    </el-icon>
                  </div>
                  <img v-img-url="element.image" alt="" />
                </div>
              </div>
            </template>
          </draggable>
          <div>
            <el-button icon="Plus"
              @click.stop="$parent.openProduct(curItem.data, true)">{{ $t('page.select_product') }}</el-button>
          </div>
        </el-form-item>
      </template>
      <!--组件样式-->
      <div class="form-chink"></div>
      <div class="f16 gray3 form-subtitle">{{ $t('page.component_style') }}</div>
      <!--上下边距-->
      <div class="form-item">
        <div class="form-label">{{ $t('page.margin_top_label') }}</div>
        <el-slider v-model="curItem.style.paddingTop" size="small" show-input :show-input-controls="false"
          input-size="small"></el-slider>
      </div>
      <!--上下边距-->
      <div class="form-item">
        <div class="form-label">{{ $t('page.margin_bottom_label') }}</div>
        <el-slider v-model="curItem.style.paddingBottom" size="small" show-input :show-input-controls="false"
          input-size="small"></el-slider>
      </div>
      <!--左右边距-->
      <div class="form-item">
        <div class="form-label">{{ $t('page.margin_lr_label') }}</div>
        <el-slider max="15" v-model="curItem.style.paddingLeft" size="small" show-input :show-input-controls="false"
          input-size="small"></el-slider>
      </div>
      <!--上圆角-->
      <div class="form-item">
        <div class="form-label">{{ $t('page.border_radius_top_label') }}</div>
        <el-slider v-model="curItem.style.topRadio" size="small" show-input :show-input-controls="false"
          input-size="small"></el-slider>
      </div>
      <!--下圆角-->
      <div class="form-item">
        <div class="form-label">{{ $t('page.border_radius_bottom_label') }}</div>
        <el-slider v-model="curItem.style.bottomRadio" size="small" show-input :show-input-controls="false"
          input-size="small"></el-slider>
      </div>
      <!-- 图片宽度 -->
       <div class="form-item">
        <div class="form-label">图片大小：</div>
        <el-slider :min="100"  :max="170" v-model="curItem.style.imgWidth" size="small" show-input :show-input-controls="false"
          input-size="small"></el-slider>
      </div>
      <div class="form-item" v-if="curItem.style.productName">
        <div class="form-label">{{ $t('page.product_name_label') }}</div>
        <div class="flex-1 d-s-c" style="height: 36px">
          <el-color-picker size="default" v-model="curItem.style.productNameColor"></el-color-picker>
          <el-input class="ml10" v-model="curItem.style.productNameColor" :placeholder="$t('page.transparent')" />
          <el-button style="margin-left: 10px"
            @click.stop="$parent.onEditorResetColor(curItem.style, 'productNameColor', '#333333')" type="primary" link>
            {{ $t('page.reset') }}

          </el-button>
        </div>
      </div>
      <div class="form-item" v-if="curItem.style.productPrice">
        <div class="form-label">{{ $t('page.sale_price_label') }}</div>
        <div class="flex-1 d-s-c" style="height: 36px">
          <el-color-picker size="default" v-model="curItem.style.productPriceColor"></el-color-picker>
          <el-input class="ml10" v-model="curItem.style.productPriceColor" :placeholder="$t('page.transparent')" />
          <el-button style="margin-left: 10px"
            @click.stop="$parent.onEditorResetColor(curItem.style, 'productPriceColor', '#FF4C01')" type="primary" link>
            {{ $t('page.reset') }}

          </el-button>
        </div>
      </div>
      <div class="form-item" v-if="curItem.style.productLineprice">
        <div class="form-label">{{ $t('page.line_price_label') }}</div>
        <div class="flex-1 d-s-c" style="height: 36px">
          <el-color-picker size="default" v-model="curItem.style.productLineColor"></el-color-picker>
          <el-input class="ml10" v-model="curItem.style.productLineColor" :placeholder="$t('page.transparent')" />
          <el-button style="margin-left: 10px"
            @click.stop="$parent.onEditorResetColor(curItem.style, 'productLineColor', '#999999')" type="primary" link>
            {{ $t('page.reset') }}

          </el-button>
        </div>
      </div>
      <div class="form-item">
        <div class="form-label">{{ $t('page.footer_bg_label') }}</div>
        <div class="flex-1 d-s-c" style="height: 36px">
          <el-color-picker size="default" v-model="curItem.style.bgcolor"></el-color-picker>
          <el-input class="ml10" v-model="curItem.style.bgcolor" :placeholder="$t('page.transparent')" />
          <el-button style="margin-left: 10px"
            @click.stop="$parent.onEditorResetColor(curItem.style, 'bgcolor', '#f2f2f2')" type="primary"
            link>{{ $t('page.reset') }}</el-button>
        </div>
      </div>
      <div class="form-item">
        <div class="form-label">{{ $t('page.component_bg_label') }}</div>
        <div class="flex-1 d-s-c" style="height: 36px">
          <el-color-picker size="default" v-model="curItem.style.background"></el-color-picker>
          <el-input class="ml10" v-model="curItem.style.background" :placeholder="$t('page.transparent')" />
          <el-button style="margin-left: 10px"
            @click.stop="$parent.onEditorResetColor(curItem.style, 'background', '#ffffff')" type="primary"
            link>{{ $t('page.reset') }}</el-button>
        </div>
      </div>
      <div class="form-item">
        <div class="form-label">卡片背景</div>
        <div class="flex-1 d-s-c" style="height: 36px">
          <el-color-picker size="default" v-model="curItem.style.cardBgColor"></el-color-picker>
          <el-input class="ml10" v-model="curItem.style.cardBgColor" :placeholder="$t('page.transparent')" />
          <el-button style="margin-left: 10px"
            @click.stop="$parent.onEditorResetColor(curItem.style, 'cardBgColor', '#ffffff')" type="primary"
            link>{{ $t('page.reset') }}</el-button>
        </div>
      </div>
        <el-form-item label="标题占用行数">
        <el-radio-group v-model="curItem.style.lineClamp">
          <el-radio-button :label="1">一行</el-radio-button>
          <el-radio-button :label="2">两行</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <!-- 商品排序 -->
      <el-form-item :label="$t('page.product_sort')">
        <el-radio-group v-model="curItem.style.display">
          <el-radio-button :label="'list'">{{ $t('page.list_tile') }}</el-radio-button>
          <!-- <el-radio-button :label="'slide'" :disabled="curItem.style.column == 1">横向滑动</el-radio-button> -->
        </el-radio-group>
      </el-form-item>
      <!-- 分列数量 -->
      <el-form-item :label="$t('page.columns_count')">
        <el-radio-group v-model="curItem.style.column">
          <!-- <el-radio-button :label="1" :disabled="curItem.style.display == 'slide'">单列</el-radio-button> -->
          <el-radio-button :label="2">{{ $t('page.two_columns') }}</el-radio-button>
          <!-- <el-radio-button :label="3">三列</el-radio-button> -->
        </el-radio-group>
      </el-form-item>
      <!--组件样式-->
      <div class="form-chink"></div>
      <div class="f16 gray3 form-subtitle">{{ $t('page.content_display') }}</div>
      <!-- 商品名称 -->
      <el-form-item :label="$t('page.product_name_label')">
        <el-radio-group v-model="curItem.style.show.productName">
          <el-radio-button :label="1">{{ $t('page.show') }}</el-radio-button>
          <el-radio-button :label="0">{{ $t('page.hide') }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <!-- 商品名称 -->
      <el-form-item :label="$t('page.product_price')">
        <el-radio-group v-model="curItem.style.show.productPrice">
          <el-radio-button :label="1">{{ $t('page.show') }}</el-radio-button>
          <el-radio-button :label="0">{{ $t('page.hide') }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <!--划线价-->
      <el-form-item :label="$t('page.line_price_label')">
        <el-radio-group v-model="curItem.style.show.linePrice">
          <el-radio-button :label="1">{{ $t('page.show') }}</el-radio-button>
          <el-radio-button :label="0">{{ $t('page.hide') }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import ProductApi from "@/api/product.js";
import draggable from "vuedraggable";
export default {
  components: {
    draggable
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*商品类别*/
      CategoryList: [],
      /*当前选中的*/
      currCategory: [],
      productNameShow: false,
      productPriceShow: false,
      linePriceShow: false,
      sellingPointShow: false,
      productSalesShow: false
    };
  },
  props: ["curItem", "selectedIndex", "opts"],
  created() {
    /*获取列表*/
    this.getData();
  },
  watch: {
    selectedIndex: function (n, o) {
      this.currCategory = this.currCategoryAuto(this.CategoryList);
    }
  },
  methods: {
    /*获取商品*/
    getData() {
      let self = this;
      ProductApi.catList({
        pageId: self.pageId
      }, true).then(res => {
        self.CategoryList = res.data;
        self.currCategory = self.currCategoryAuto(res.data);
        self.loading = false;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*选择默认*/
    currCategoryAuto(list) {
      let arr = [];
      for (let i = 0; i < list.length; i++) {
        let item = list[i];
        if (item.categoryId == this.curItem.params.auto.category) {
          arr.push(item.categoryId);
          break;
        } else {
          if (Object.prototype.toString.call(item.child) == "[object Array]" && item.child.length > 0) {
            for (let j = 0; j < item.child.length; j++) {
              if (item.child[j].categoryId == this.curItem.params.auto.category) {
                arr.push(item.categoryId);
                arr.push(item.child[j].categoryId);
                break;
              }
            }
          }
        }
      }
      return arr;
    },
    /*检查*/
    check(checked, name) {
      let value = checked ? 1 : 0;
      this.curItem.style.show[name] = value;
    },
    /*选择类别*/
    changeCategory(e) {
      let item = this.$refs["cascader"].getCheckedNodes();
      this.curItem.params.auto.category = item[0].data.categoryId;
    }
  }
};
</script>

<style lang="scss">
.choice-product-list {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
}

.choice-product-list .item {
  position: relative;
  width: 80px;
  height: 80px;
  margin-right: 10px;
  margin-bottom: 10px;
  border: 1px solid #dddddd;
}

.choice-product-list .item .delete-box {
  position: absolute;
  width: 20px;
  height: 20px;
  top: -10px;
  right: -10px;
  font-size: 20px;
  cursor: pointer;
  color: #999999;
}

.choice-product-list .item .delete-box:hover {
  color: rgb(255, 51, 0);
}

.choice-product-list .item.plus-btn {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.choice-product-list .item.plus-btn>i {
  font-size: 30px;
  color: #cccccc;
}

.choice-product-list img {
  width: 100%;
  height: 100%;
}
</style>
